package com.calmcar.adas.serverman;

import android.content.Context;

import com.calmcar.adas.apiserver.AdasConf;
import com.calmcar.adas.apiserver.model.AdasPoint;
import com.calmcar.adas.apiserver.model.AdasRect;
import com.calmcar.adas.apiserver.model.CdwDetectInfo;
import com.calmcar.adas.apiserver.model.FrontCarInfo;
import com.calmcar.adas.apiserver.model.LdwDetectInfo;
import com.calmcar.adas.conf.Constant;
import com.calmcar.adas.dao.CalmCarDetectData;
import com.calmcar.adas.util.CarSafeDistanceUtil;
import com.calmcar.adas.util.FrontCarLaunchUtil;

import java.util.List;

/**
 * Created by lelexiao on 2018/5/4.
 */

public class CdwServerManager {
    Context mContext;
    FrontCarLaunchUtil frontCarLaunchUtil;
    CarSafeDistanceUtil carSafeDistanceUtil;

    public CdwDetectInfo getCdwDetectInfo() {
        return cdwDetectInfo;
    }

    public void setCdwDetectInfo(CdwDetectInfo cdwDetectInfo) {
        this.cdwDetectInfo = cdwDetectInfo;
    }

    CdwDetectInfo cdwDetectInfo;

    public CdwServerManager(Context mContext) {
        this.mContext = mContext;
        frontCarLaunchUtil = new FrontCarLaunchUtil();
        carSafeDistanceUtil = new CarSafeDistanceUtil();
    }
    long lastTime=0;
    public void carDataCallBack(CalmCarDetectData calmCarDetectData ,int carType ) {
                CdwDetectInfo  cdwDetectInfoTemp= carDataConvert(calmCarDetectData );
                if(cdwDetectInfoTemp!=null){
                    lastTime=System.currentTimeMillis();
                    cdwDetectInfo=cdwDetectInfoTemp;
                    cdwDetectInfo.setShowType(carType);
                }else{
                    //增加缓存机制
                    if(System.currentTimeMillis()-lastTime> 600 ){
                        cdwDetectInfo=null;
                    }
                }
    }


    //前车判断方法-前车预警
    public FrontCarInfo carDetectProcessForFront(CdwDetectInfo cdwDetectInfo, LdwDetectInfo ldwDetectInfo) {
        AdasRect curRect = FrontCarLaunchUtil.checkIsFrontCarForAPI(cdwDetectInfo,null);
        if (curRect == null) return null;
        FrontCarInfo frontCarInfo = new FrontCarInfo();
        frontCarInfo.setCarRect(curRect);
        frontCarInfo.setCarDis(curRect.getBr().getY());
        frontCarInfo.setAbsDis(curRect.getAbsDis());
        return frontCarInfo;
    }


    //前车启动-前车判断
    public FrontCarInfo carDetectProcessForFrontHalf (CdwDetectInfo cdwDetectInfo, LdwDetectInfo ldwDetectInfo) {
        AdasRect curRect = FrontCarLaunchUtil.checkIsFrontCarForAPIHalf(cdwDetectInfo,null);
        if (curRect == null) return null;
        FrontCarInfo frontCarInfo = new FrontCarInfo();
        frontCarInfo.setCarRect(curRect);
        frontCarInfo.setCarDis(curRect.getBr().getY());
        return frontCarInfo;

    }

    //前车启动
    long curTime=0;
    double oldDis=1000;
    double baseDis=1000;
    boolean isStill=false;
    long lastLunchTime=-1;
    public Boolean carDetectProcessForFrontLaunch(FrontCarInfo frontCarInfo, double rate) {
        if(AdasConf.RUN_MODE==0){//45
            if (frontCarInfo == null) return false;
           //车的位置不变
            if(baseDis==1000){
                baseDis=frontCarInfo.getAbsDis();
                return false;
            }
            double curDis=frontCarInfo.getAbsDis();
            if((int)curDis==(int)baseDis){
                isStill=true;
            }
            if(curDis> baseDis+1.5  && isStill ){
                baseDis=1000;
                isStill=false;
                return  true;
            }else{
                return false;
            }
        }else{
            if(rate==3.6){
                return false;
            }
            if(rate>1){
                if(lastLunchTime==-1){
                    lastLunchTime=System.currentTimeMillis();
                }
            }else{
                lastLunchTime=-1;
            }

            if (rate > 1) {
                if(System.currentTimeMillis()-lastLunchTime<3000){
                    if(curTime==0){
                        curTime=System.currentTimeMillis();
                    }
                    if(frontCarInfo==null){
                        return false;
                    }else{
                        if(frontCarInfo.getAbsDis()>15)  return false;
                        return frontCarLaunchUtil.checkFrontCarLaunchNew(frontCarInfo);
                    }
                }else{
                    curTime=0;
                    oldDis=1000;
                    return false;
                }
            } else {
                if(curTime==0){
                    curTime=System.currentTimeMillis();
                }
                if(frontCarInfo==null){
                    return false;
                }else{

                    if(frontCarInfo.getAbsDis()>15) return false;

                    return frontCarLaunchUtil.checkFrontCarLaunchNew(frontCarInfo);

                }
            }
        }
    }

    public void setRateState(double rate ){
        if (rate > 3.6) {
            frontCarLaunchUtil.setInit(true);
        }
    }
    //车距防碰撞预警
    public int carDetectProcessForFrontDistance(FrontCarInfo frontCarInfo, double rate) {
        if (frontCarInfo == null) return 0;
        if(!AdasConf.CHECK_OK){
            return 0;
        }
        if (rate <= Constant.CAR_SPEED_FRONT_THRESHOLD) {
            return 0;
        } else {
            if(AdasConf.RUN_MODE==0){
                return carSafeDistanceUtil.checkIsValidSafeABSDistanceForAPITest(frontCarInfo.getCarRect().getAbsDis(), rate);
            }else{
                return carSafeDistanceUtil.checkIsValidSafeABSDistanceForAPI(frontCarInfo.getCarRect().getAbsDis(), rate);
            }
        }

    }
//   //逻辑重构
//    public void sendFrameData(CvCameraViewFrame data , double rate ) {
//        if (AdasConf.CAR_DETECT_OPEN) {
//            adasServicePool.sendCarData(data);
//        }else {
//            setCdwDetectInfo(null);
//        }
//    }

//    public CdwDetectInfo carDetectProcessForCameraView(CvCameraViewFrame  data, double rate ) {
//        if (AdasConf.CAR_DETECT_OPEN) {
//            cdwDetectInfo = cdwServer.carDetectProcessForCameraView(data);
//            return cdwDetectInfo;
//        }else {
//            setCdwDetectInfo(null);
//            return null;
//        }
//    }

    public CdwDetectInfo carDataConvert(CalmCarDetectData carinfoBean ){
        CdwDetectInfo cdwDetectInfoNew=new CdwDetectInfo();
        if( carinfoBean==null ) return null;
        List< CalmCarDetectData.CarPointBean> carsArray = carinfoBean .getCarPoint();

        if(carsArray ==null ) return null;
        for(int m=0;m< carsArray.size();m++){
            CalmCarDetectData.CarPointBean  cur=carsArray.get(m);
            AdasRect adasRect=new AdasRect(
                    new AdasPoint( cur.getX0(), cur.getY0()),
                    new AdasPoint (cur.getX1(),cur.getY1())
            );
            double frontCarWidth=cur.getX1()-cur.getX0();
            double adsDis=convertImageDis(1.8,frontCarWidth);
            adasRect.setAbsDis(adsDis);
            if(!AdasConf.CHECK_OK){
                adasRect.setShowDis(false);
            }else{
                adasRect.setShowDis(true);
            }
            cdwDetectInfoNew.carRects.add(adasRect);
        }
        return cdwDetectInfoNew;
    }



    public  double convertImageDis(double carWidth,double frontCarWidth){
        double result=0;
        result=AdasConf.CONVERT_PARA *  (carWidth/frontCarWidth)-AdasConf.CAR_HEAD;
        return result;
    }

}